﻿// Learn more about F# at http://fsharp.net

module HashAlgorithm

open System.Security.Cryptography
open System.Text


let ToHexString (arr:byte[]) =
    let sb =new StringBuilder()
    for b in arr do
        sb.Append(b.ToString("x2"))|>ignore
    sb.ToString()
        

///返回加密后的byte数组（字节流）
let private getHexBytes  (str:string,algorithm:HashAlgorithm) : byte [] =
    algorithm.ComputeHash(Encoding.Default.GetBytes(str))
        
///返回字符串表示形式的加密数据
///字符串形式的表示主要给将密码保存在文本中，且不能使用二进制表示时使用
let private getHexString (str:string,algorithm:HashAlgorithm) :string =
    let bs = getHexBytes(str,algorithm)
    bs |> ToHexString
///ftp zilla软件只能使用字符串表示形式的Md5
let  getHex_MD5 (str:string):string =
    getHexString (str,MD5.Create())
///产生byte[] Length=64
let  getHex_Sha512 (str:string) =
    getHexBytes (str,SHA512.Create())       

///默认使用sha512
let getHex  = fun str -> getHex_Sha512 str

///简单随机数生成
let randomWord count =
    use r =RandomNumberGenerator.Create()
    let bs =Array.create count 0uy
    r.GetBytes bs
    let sb= new StringBuilder(count)
    bs
    |> Array.map(fun b-> b % 26uy+97uy)
    |> Array.map (fun b -> char b)
    |> sb.Append
    |> ignore
    sb.ToString()

   



